﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Text;

    public class ErrorsApp : IErrors
    {
        private string dbname = "Errors";

        public void Add(ErrorsEnt model)
        {
            string str = ConfigurationManager.AppSettings["IsUseError"];
            if (str.ToLower() != "false")
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("insert into T_Errors(");
                builder.Append("ChainNum,ErrorName,ErrorInfo,ErrorPage,ErrorMethod,UserName,ErrorTime");
                builder.Append(")");
                builder.Append(" values (");
                builder.Append("" + model.ChainNum + ",");
                builder.Append("'" + model.ErrorName + "',");
                builder.Append("'" + model.ErrorInfo + "',");
                builder.Append("'" + model.ErrorPage + "',");
                builder.Append("'" + model.ErrorMethod + "',");
                builder.Append("'" + model.UserName + "',");
                builder.Append("'" + model.ErrorTime + "'");
                builder.Append(")");
                DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
            }
        }

        public void Delete(int ErrorsId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Errors ");
            builder.Append(" where ErrorsId=" + ErrorsId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Errors ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by ErrorsId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperOledb.GetMaxID(this.dbname, "ErrorsId", "T_Errors", "");
        }

        public ErrorsEnt GetModel(int ErrorsId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ErrorsId,ChainNum,ErrorName,ErrorInfo,ErrorPage,ErrorMethod,UserName,ErrorTime from T_Errors ");
            builder.Append(" where ErrorsId=" + ErrorsId);
            ErrorsEnt ent = new ErrorsEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.ErrorsId = ErrorsId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.ErrorName = table.Rows[0]["ErrorName"].ToString();
                ent.ErrorInfo = table.Rows[0]["ErrorInfo"].ToString();
                ent.ErrorPage = table.Rows[0]["ErrorPage"].ToString();
                ent.ErrorMethod = table.Rows[0]["ErrorMethod"].ToString();
                ent.UserName = table.Rows[0]["UserName"].ToString();
                if (table.Rows[0]["ErrorTime"].ToString() != "")
                {
                    ent.ErrorTime = DateTime.Parse(table.Rows[0]["ErrorTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "ErrorsId";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Errors ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "ErrorsId");
        }

        public void Update(ErrorsEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Errors set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("ErrorName='" + model.ErrorName + "',");
            builder.Append("ErrorInfo='" + model.ErrorInfo + "',");
            builder.Append("ErrorPage='" + model.ErrorPage + "',");
            builder.Append("ErrorMethod='" + model.ErrorMethod + "',");
            builder.Append("UserName='" + model.UserName + "',");
            builder.Append("ErrorTime='" + model.ErrorTime + "'");
            builder.Append(" where ErrorsId=" + model.ErrorsId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

